package com.wwtech.epp.ui.adapter.work;

import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wwtech.epp.R;
import com.wwtech.epp.entity.appmessage.AppMessage;
import com.wwtech.epp.utils.UIUtils;
import com.wwtech.epp.view.GlideCircleTransform;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 作者：Android on 2017/10/17
 * 邮箱：365941593@qq.com
 * 描述：
 */

public class AppMessageAdapter extends BaseQuickAdapter<AppMessage, BaseViewHolder> {

    public AppMessageAdapter(@Nullable List<AppMessage> data) {
        super(R.layout.item_app_message, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, AppMessage appMessage) {
        baseViewHolder.setText(R.id.name, appMessage.getAppName());
        baseViewHolder.setText(R.id.content, appMessage.getContent());
//        if (appMessage.getAppName().equals("项目管理") ||appMessage.getAppName().equals("月报")) {
//            baseViewHolder.setTextColor(R.id.name, mContext.getResources().getColor(R.color.yuebao));
//        } else if (appMessage.getAppName().equals("周报")) {
//            baseViewHolder.setTextColor(R.id.name, mContext.getResources().getColor(R.color.zhoubao));
//        } else if (appMessage.getAppName().equals("请假")) {
//            baseViewHolder.setTextColor(R.id.name, mContext.getResources().getColor(R.color.qingjia));
//        } else {
//            baseViewHolder.setTextColor(R.id.name, mContext.getResources().getColor(R.color.main_color));
//        }
        baseViewHolder.setText(R.id.time, setRecivedTime(appMessage.getRecivedTime()));
        baseViewHolder.setVisible(R.id.desc, appMessage.getActionUrl().equals("") ? false:true);
        if (baseViewHolder.getLayoutPosition() == getData().size() - 1) {
            LinearLayout root = baseViewHolder.getView(R.id.root);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(root.getLayoutParams());
            lp.setMargins(0, UIUtils.dip2px(30), 0, UIUtils.dip2px(50));
            root.setLayoutParams(lp);
        } else {
            LinearLayout root = baseViewHolder.getView(R.id.root);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(root.getLayoutParams());
            lp.setMargins(0, UIUtils.dip2px(30), 0, 0);
            root.setLayoutParams(lp);
        }
        ImageView imageView = baseViewHolder.getView(R.id.avater);
        Glide.with(mContext)
                .load(appMessage.getResourceUrl())
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }

    private String setRecivedTime(long timeStamp) {
        SimpleDateFormat sdr = new SimpleDateFormat("MM月dd HH:mm:ss");
        String times = sdr.format(new Date(timeStamp));
        if (Calendar.getInstance().getTimeInMillis() - timeStamp > 24 * 60 * 60 * 1000 * 2) {
            return times.substring(0, 11);
        } else if (Calendar.getInstance().getTimeInMillis() - timeStamp > 24 * 60 * 60 * 1000) {
            return "昨天 " + times.substring(6, 11);
        } else {
            return times.substring(6, 11);
        }
    }
}
